//
//  PieCharts.swift
//  HuaYun
//
//  Created by enghou on 2018/7/20.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

import Charts


class VDChartAxisValueFormatter: IValueFormatter {
    func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
        return String.init(format: "%.2f%%", value);
    }
}

class PieCharts: UIView {

    var months = ["1-9", "10-19", "20-29", "30-39","40-49", "50-59", "60-69", "70以上"]
    
    let _pieChartView = PieChartView.init(frame: CGRect.init(x: 0, y: 20, width: 340, height: 340));
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        _pieChartView.backgroundColor = UIColor.init(red: 230/255.0, green: 253/255.0, blue: 253/255.0, alpha: 1.0);
        
        _pieChartView.setExtraOffsets(left: 40, top: 10, right: 40, bottom: 30);//设置这块饼的位置
        
        _pieChartView.chartDescription?.text = "饼状图示例";//描述文字
        
        _pieChartView.chartDescription?.font = UIFont.systemFont(ofSize: 12.0);//字体
        
        _pieChartView.chartDescription?.textColor = UIColor.black;//颜色
        
        _pieChartView.usePercentValuesEnabled = true;//转化为百分比
        
        _pieChartView.dragDecelerationEnabled = false;//我把拖拽效果关了
        
        _pieChartView.drawEntryLabelsEnabled = true;//显示区块文本
        
        _pieChartView.entryLabelFont = UIFont.systemFont(ofSize: 10);//区块文本的字体
        
        _pieChartView.entryLabelColor = UIColor.white;
        
        _pieChartView.drawSlicesUnderHoleEnabled = true;
        
        _pieChartView.drawHoleEnabled = true;//这个饼是空心的
        
        _pieChartView.holeRadiusPercent = 0.382//空心半径黄金比例
        
        _pieChartView.holeColor = UIColor.white;//空心颜色设置为白色
        
        _pieChartView.transparentCircleRadiusPercent = 0.5;//半透明空心半径
        
        _pieChartView.drawCenterTextEnabled = true;//显示中心文本
        
        _pieChartView.centerText = "饼状图";//设置中心文本,你也可以设置富文本`centerAttributedText`
        
        //图例样式设置
        _pieChartView.legend.maxSizePercent = 1;//图例的占比
        
        _pieChartView.legend.form = .circle//图示：原、方、线
        
        _pieChartView.legend.formSize = 8;//图示大小
        
        _pieChartView.legend.formToTextSpace = 4;//文本间隔
        
        _pieChartView.legend.font = UIFont.systemFont(ofSize: 8);
        
        _pieChartView.legend.textColor = UIColor.gray;
        
        _pieChartView.legend.horizontalAlignment = .left;
        
        _pieChartView.legend.verticalAlignment = .top;
        
        _pieChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0, easingOption: .easeInBack);
        
        self.addSubview(_pieChartView)
        
        
        drawPieChartView()
        
    }
    
    func drawPieChartView() {
        let titles = ["红","黄","蓝色","橙","绿"];
        let yData = [20,30,10,40,60];
        var yVals = [PieChartDataEntry]();
        for i in 0...titles.count-1 {
            let entry = PieChartDataEntry.init(value: Double(yData[i]), label: titles[i]);
            yVals.append(entry);
        }
        
        let dataSet = PieChartDataSet.init(values: yVals, label: "");
        dataSet.colors = [UIColor.red,UIColor.yellow,UIColor.blue,UIColor.orange,UIColor.green];
        //设置名称和数据的位置 都在内就没有折线了哦
        dataSet.xValuePosition = .insideSlice;
        dataSet.yValuePosition = .outsideSlice;
        dataSet.sliceSpace = 1;//相邻块的距离
        dataSet.selectionShift = 6.66;//选中放大半径
        //指示折线样式
        dataSet.valueLinePart1OffsetPercentage = 0.8 //折线中第一段起始位置相对于区块的偏移量, 数值越大, 折线距离区块越远
        dataSet.valueLinePart1Length = 0.8 //折线中第一段长度占比
        dataSet.valueLinePart2Length = 0.4 //折线中第二段长度最大占比
        dataSet.valueLineWidth = 1 //折线的粗细
        dataSet.valueLineColor = UIColor.brown //折线颜色
        
        let data = PieChartData.init(dataSets: [dataSet]);
        data.setValueFormatter(VDChartAxisValueFormatter.init());//格式化值（添加个%）
        data.setValueFont(UIFont.systemFont(ofSize: 10.0));
        data.setValueTextColor(UIColor.lightGray);
        _pieChartView.data = data;
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
